package com.hmyd.hmyd_android.ui;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.RepaymentRecord;
import com.hmyd.hmyd_android.dto.RepaymentRecordPage;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 投资类型详情
 * @author Administrator
 *
 */
@ContentView(R.layout.activity_investment_type_detail)
public class InvestmentTypeDetailActivity extends BasicActivity{
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;
	
	private final int DETAIL = 1;
	
	private static final String ITEMRECORDID = "itemRecordId";
	private String itemRecordId;
	
	@ViewInject(R.id.table)
	private LinearLayout table;
	@ViewInject(R.id.totalRealIncome)
	private TextView totalRealIncome;
	@ViewInject(R.id.totalExpectIncome)
	private TextView totalExpectIncome;
	
	RepaymentRecordPage repaymentRecordPage;
	
	public static Intent getIntent(Context context,String itemRecordId){
		Intent intent = new Intent(context,InvestmentTypeDetailActivity.class);
		intent.putExtra(ITEMRECORDID, itemRecordId);
		return intent;
	}
	
	private void onRequest(){
		RequestParams params = new RequestParams();
		params.addBodyParameter("itemRecordId", itemRecordId);
		
		onRequest(DETAIL, UrlConstant.REPAYMENTRECORD, HttpMethod.GET, params);
	}

	@Override
	public void onPostSuccess(int postId, String msg) {
		Gson gson = new Gson();
		switch (postId) {
		case DETAIL:
			java.lang.reflect.Type type2 = new TypeToken<RepaymentRecordPage>() {}.getType();
			repaymentRecordPage = gson.fromJson(msg, type2);
			if(repaymentRecordPage!=null){
				totalRealIncome.setText(Utils.amountConversionFormat(repaymentRecordPage.totalRealIncome));
				totalExpectIncome.setText(Utils.amountConversionFormat(repaymentRecordPage.totalExpectIncome));
				
				LayoutInflater mInflater = LayoutInflater.from(this);
				
				for (int i = 0; i < repaymentRecordPage.records.size(); i++) {
					RepaymentRecord repaymentRecord = repaymentRecordPage.records.get(i);
					
					View lineView = mInflater.inflate(R.layout.view_investment_back_section_line, null);
					table.addView(lineView);
					
					if((i+1)!=repaymentRecordPage.records.size()){
						View view_withdrawals_fee = mInflater.inflate(R.layout.view_investment_type_detail, null);
						TextView repayTime = (TextView)view_withdrawals_fee.findViewById(R.id.repayTime);
						TextView expectIncome = (TextView)view_withdrawals_fee.findViewById(R.id.expectIncome);
						TextView userFee = (TextView)view_withdrawals_fee.findViewById(R.id.userFee);
						TextView realIncome = (TextView)view_withdrawals_fee.findViewById(R.id.realIncome);
						TextView payStatus = (TextView)view_withdrawals_fee.findViewById(R.id.payStatus);
						
						repayTime.setText(Utils.getDateStringDay(repaymentRecord.repayTime));
						expectIncome.setText(Utils.amountConversionFormat((repaymentRecord.expectIncome)));
						userFee.setText("-"+Utils.amountConversionFormat((repaymentRecord.userFee)));
						realIncome.setText(Utils.amountConversionFormat((repaymentRecord.realIncome)));
						payStatus.setText(repaymentRecord.payStatus);
						
						if("还款中".equals(repaymentRecord.payStatus)){
							payStatus.setTextColor(getResources().getColor(R.color.text_orange));
						}else{
							payStatus.setTextColor(getResources().getColor(R.color.black));
						}
						
						table.addView(view_withdrawals_fee);
					}else{
						View view_withdrawals_fee = mInflater.inflate(R.layout.view_investment_type_detail_last, null);
						TextView repayTime = (TextView)view_withdrawals_fee.findViewById(R.id.repayTime);
						TextView expectIncome = (TextView)view_withdrawals_fee.findViewById(R.id.expectIncome);
						TextView userFee = (TextView)view_withdrawals_fee.findViewById(R.id.userFee);
						TextView realIncome = (TextView)view_withdrawals_fee.findViewById(R.id.realIncome);
						TextView payStatus = (TextView)view_withdrawals_fee.findViewById(R.id.payStatus);
						TextView payStatus2 = (TextView)view_withdrawals_fee.findViewById(R.id.payStatus2);
						TextView principal = (TextView)view_withdrawals_fee.findViewById(R.id.principal);
						
						repayTime.setText(Utils.getDateStringDay(repaymentRecord.repayTime));
						expectIncome.setText(Utils.amountConversionFormat((repaymentRecord.expectIncome)));
						userFee.setText("-"+Utils.amountConversionFormat((repaymentRecord.userFee)));
						realIncome.setText(Utils.amountConversionFormat((repaymentRecord.realIncome)));
						payStatus.setText(repaymentRecord.payStatus);
						payStatus2.setText(repaymentRecord.payStatus);
						
						principal.setText(Utils.amountConversionFormat(repaymentRecordPage.principal));
						
						if("还款中".equals(repaymentRecord.payStatus)){
							payStatus.setTextColor(getResources().getColor(R.color.text_orange));
							payStatus2.setTextColor(getResources().getColor(R.color.text_orange));
						}else{
							payStatus.setTextColor(getResources().getColor(R.color.black));
							payStatus2.setTextColor(getResources().getColor(R.color.black));
						}
						
						table.addView(view_withdrawals_fee);
					}
					
				}
				
			}
			
			break;
		default:
			break;
		}
	}

	@Override
	public void onPostFailure(int postId, String msg) {
		
	}

	@Override
	protected void setParams() {
		home_title_txt.setText(getString(R.string.investment_history_detail));
		home_title.setOnTouchListener(touch);
		home_right.setVisibility(View.VISIBLE);
		home_right.setText(getString(R.string.investment_contract_download));
		
		itemRecordId = getIntent().getStringExtra(ITEMRECORDID);
		
		onRequest();
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {
		if(repaymentRecordPage!=null && !TextUtils.isEmpty(repaymentRecordPage.contractUrl)){
			Utils.jumpExplorer(this, repaymentRecordPage.contractUrl);
		}
	}
}
